
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.EmpWorkCalendar;
import com.cmc.dao.EmpWorkCalendarDao;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class EmpWorkCalendarDaoHibernate extends BaseDaoHibernate implements EmpWorkCalendarDao {

    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#getEmpWorkCalendars(com.cmc.model.EmpWorkCalendar)
     */
    public List getEmpWorkCalendars(final EmpWorkCalendar empWorkCalendar) {
        //return getHibernateTemplate().find("from EmpWorkCalendar");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (empWorkCalendar == null) {
            return getHibernateTemplate().find("from EmpWorkCalendar");
        } else {
            // filter on properties set in the empWorkCalendar
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(empWorkCalendar).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(EmpWorkCalendar.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#getEmpWorkCalendar(Long id)
     */
    public EmpWorkCalendar getEmpWorkCalendar(final Long id) {
        EmpWorkCalendar empWorkCalendar = (EmpWorkCalendar) getHibernateTemplate().get(EmpWorkCalendar.class, id);
        if (empWorkCalendar == null) {
            log.warn("uh oh, empWorkCalendar with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(EmpWorkCalendar.class, id);
        }

        return empWorkCalendar;
    }

    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#saveEmpWorkCalendar(EmpWorkCalendar empWorkCalendar)
     */    
    public void saveEmpWorkCalendar(final EmpWorkCalendar empWorkCalendar) {
        getHibernateTemplate().saveOrUpdate(empWorkCalendar);
    }

    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#removeEmpWorkCalendar(Long id)
     */
    public void removeEmpWorkCalendar(final Long id) {
        getHibernateTemplate().delete(getEmpWorkCalendar(id));
    }
    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#getEmpWorkCalendars(com.cmc.model.EmpWorkCalendar)
     */
    public List getEmpWorkCalendarsByStartEndDate(final String empno, final String startdate, final String enddate) {
        //return getHibernateTemplate().find("from EmpWorkCalendar");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/

        // filter on properties set in the empWorkCalendar
        HibernateCallback callback = new HibernateCallback() {
           public Object doInHibernate(Session session) throws HibernateException {
            	Criteria criteria = session.createCriteria(EmpWorkCalendar.class);
               	criteria.add(Expression.eq("empNo",empno));
				criteria.add(Expression.between("workCalDate",startdate,enddate));
				criteria.addOrder(Order.asc("workCalDate"));                    
				return criteria.list();
           }
         };
         return (List) getHibernateTemplate().execute(callback);
    }
    /**
     * @see com.cmc.dao.EmpWorkCalendarDao#getEmpWorkCalendars(com.cmc.model.EmpWorkCalendar)
     */
    public List getEmpWorkCalendarsByStartEndDateWorkTypeId(final String empno, final String startdate, final String enddate, final String worktypeid) {
        //return getHibernateTemplate().find("from EmpWorkCalendar");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/

        // filter on properties set in the empWorkCalendar
        HibernateCallback callback = new HibernateCallback() {
           public Object doInHibernate(Session session) throws HibernateException {
            	Criteria criteria = session.createCriteria(EmpWorkCalendar.class);
               	criteria.add(Expression.eq("empNo",empno));
               	criteria.add(Expression.eq("workTypeId",worktypeid));
               	criteria.add(Expression.between("workCalDate",startdate,enddate));
				criteria.addOrder(Order.asc("workCalDate"));                    
				return criteria.list();
           }
         };
         return (List) getHibernateTemplate().execute(callback);
    }
  
}
